package com.hyt.it.ogt.kq.service.gov.api.platform;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.it.ogt.kq.service.gov.api.platform.model.param.TaskInfoPageParam;
import com.hyt.it.ogt.kq.service.gov.api.platform.model.vo.TaskInfoPageVO;
import com.hyt.it.ogt.kq.service.gov.api.platform.model.vo.TaskStatisticsVO;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.service.gov.service.ITaskService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@ApiSort(value = 31)
@RequestMapping("/api/platform")
@Api(tags = "31.平台端调用接口", value = "平台端调用接口")
public class PlatformApi {

    @Resource
    private ITaskService iTaskService;

    @ApiOperation(
            value = "31.1 获取考试任务信息分页数据",
            notes = "20000:成功"
    )
    @ApiOperationSort(value = 1)
    @PostMapping("/get/page")
    public ApiResponse<Page<TaskInfoPageVO>> page(@RequestBody TaskInfoPageParam taskInfoPageParam) {
        log.info("# 获取考试任务信息分页数据参数: {}", JSON.toJSONString(taskInfoPageParam));
        try {
            Page<TaskInfoPageVO> pageVOPage = iTaskService.page(taskInfoPageParam);
            return ApiResponse.<Page<TaskInfoPageVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(pageVOPage)
                    .build();
        } catch (Exception e) {
            log.error("获取考试任务信息分页数据异常", e);
            return ApiResponse.<Page<TaskInfoPageVO>>builder()
                    .code(ResponseCode.GET_TASK_INFO_PAGE_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "31.2 绑定平台项目",
            notes = "20000:成功"
    )
    @ApiOperationSort(value = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "taskIds",
                    value = "考试任务id，多个用英文逗号分隔",
                    dataType = DataType.STRING,
                    paramType = ParamType.QUERY,
                    required = true
            )
    })
    @GetMapping("/bind")
    public ApiResponse<?> bind(@RequestParam String taskIds) {
        log.info("# 绑定平台项目参数: {}", taskIds);
        try {
            iTaskService.bindPlatform(taskIds);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (Exception e) {
            log.error("绑定平台项目异常", e);
            return ApiResponse.builder()
                    .code(ResponseCode.BIND_PLATFORM_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "31.3 解除绑定平台项目",
            notes = "20000:成功"
    )
    @ApiOperationSort(value = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "taskIds",
                    value = "考试任务id，多个用英文逗号分隔",
                    dataType = DataType.STRING,
                    paramType = ParamType.QUERY,
                    required = true
            )
    })
    @GetMapping("/unbind")
    public ApiResponse<?> unbind(@RequestParam String taskIds) {
        log.info("# 解除绑定平台项目参数: {}", taskIds);
        try {
            iTaskService.unbindPlatform(taskIds);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (Exception e) {
            log.error("解除绑定平台项目异常", e);
            return ApiResponse.builder()
                    .code(ResponseCode.UNBIND_PLATFORM_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "31.4 获取考试任务统计信息",
            notes = "20000:成功"
    )
    @ApiOperationSort(value = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "taskIds",
                    value = "考试任务id，多个用英文逗号分隔",
                    dataType = DataType.STRING,
                    paramType = ParamType.QUERY,
                    required = true
            )
    })
    @GetMapping("/get/info")
    public ApiResponse<List<TaskStatisticsVO>> info(@RequestParam String taskIds) {
        log.info("# 获取考试任务统计信息参数: {}", taskIds);
        try{
            List<TaskStatisticsVO> taskStatisticsVOList = iTaskService.listTaskStatistics(taskIds);
            return ApiResponse.<List<TaskStatisticsVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(taskStatisticsVOList)
                    .build();
        } catch (Exception e) {
            log.error("获取考试任务统计信息异常", e);
            return ApiResponse.<List<TaskStatisticsVO>>builder()
                    .code(ResponseCode.LIST_TASK_STATISTICS_EXCEPTION.getCode())
                    .build();
        }
    }

}
